package com.ruoyi.tt.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.tt.domain.TtType;
import com.ruoyi.tt.service.ITtTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 头条分类Controller
 *
 * @author 梁静茹
 * @date 2022-10-15
 */
@RestController
@RequestMapping("/tt/type")
public class TtTypeController extends BaseController {
    @Autowired
    private ITtTypeService ttTypeService;

    /**
     * 查询头条分类列表
     */
//    @PreAuthorize("@ss.hasPermi('tt:type:list')")
    @Anonymous // 不需要权限就可以访问
    @GetMapping("/list")
    public TableDataInfo list(TtType ttType) {
        startPage();
        List<TtType> list = ttTypeService.selectTtTypeList(ttType);
        return getDataTable(list);
    }

    /**
     * 导出头条分类列表
     */
//    @PreAuthorize("@ss.hasPermi('tt:type:export')")
    @Log(title = "头条分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TtType ttType) {
        List<TtType> list = ttTypeService.selectTtTypeList(ttType);
        ExcelUtil<TtType> util = new ExcelUtil<TtType>(TtType.class);
        util.exportExcel(response, list, "头条分类数据");
    }

    /**
     * 获取头条分类详细信息
     */
//    @PreAuthorize("@ss.hasPermi('tt:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ttTypeService.selectTtTypeById(id));
    }

    /**
     * 新增头条分类
     */
//    @PreAuthorize("@ss.hasPermi('tt:type:add')")
    @Log(title = "头条分类", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TtType ttType) {
        return toAjax(ttTypeService.insertTtType(ttType));
    }

    /**
     * 修改头条分类
     */
//    @PreAuthorize("@ss.hasPermi('tt:type:edit')")
    @Log(title = "头条分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TtType ttType) {
        return toAjax(ttTypeService.updateTtType(ttType));
    }

    /**
     * 删除头条分类
     */
//    @PreAuthorize("@ss.hasPermi('tt:type:remove')")
    @Log(title = "头条分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ttTypeService.deleteTtTypeByIds(ids));
    }
}
